import React, {Component} from 'react';
import {Relax} from 'iflux2';
import {Map, List} from 'immutable';

import {Row, Col} from 'antd';
import calculator from 'util/calculator';


@Relax
export default class OrderInfo extends Component {
    static defaultProps = {
        order: Map(),
        taxRate: 0,
        itemOrders: List()
    }


    render() {
        const {order} = this.props;

        return (
            <Row className="order-pay-detail">
                <Col span={9} offset={15}>
                    <Row className="pushb">
                        <Col span={12}>使用积分：</Col>
                        <Col span={12}><div className="t-right">-&yen;{(order.get('offsetMoney') || 0).toFixed(2)}</div></Col>
                    </Row>
                    <Row className="pushb font-16">
                        <Col span={12}>需付款：</Col>
                        <Col span={12}><div className="t-right t-price">&yen;{(Math.floor((this.calculatePay() || 0) * 100) / 100).toFixed(2)}</div></Col>
                    </Row>
                </Col>
            </Row>
        )
    }


    calculatePay = ()=>{
        const {order, taxRate, itemOrders} = this.props;

        let itemTotal = itemOrders.reduce((total, item)=>{
            return calculator.floatAdd(total
                , calculator.floatMul(item.get('pervalue'), item.get('itemNumber')))
        }, 0);

        let offsetMoney = order.get('offsetMoney');

        let invoiceFee = calculator.floatMul(calculator.floatSub(itemTotal, offsetMoney)
                , taxRate) / 100;

        let logisticsFee = order.get('logisticsFee');

        return calculator.floatSub(calculator.floatAdd(itemTotal
            , calculator.floatAdd(invoiceFee, logisticsFee))
            , offsetMoney);
    }
}